package com.longcai.controller.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.auth.ClyjShopsArea;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.mapper.clyjuser.ClyjUserMapper;
import com.longcai.service.clyjuser.ClyjShopsAreaService;
import com.longcai.util.OSSUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户代理地区表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-04-16
 */
@RestController
@RequestMapping("/clyjShopsArea")
public class ClyjShopsAreaController {

    @Autowired
    private ClyjShopsAreaService clyjShopsAreaService;
    @Autowired
    private ClyjUserMapper clyjUserMapper;

    /**
     * 获取所有地区
     *
     * @return
     */
    @RequestMapping("/getAreaList")
    @ResponseBody
    public List<ClyjShopsArea> getAreaList() {
        ApiResult<List<ClyjShopsArea>> result = new ApiResult<List<ClyjShopsArea>>();
        try {
            QueryWrapper<ClyjShopsArea> queryWrapper = new QueryWrapper<ClyjShopsArea>();
            result.success();
            return clyjShopsAreaService.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return null;
    }


    /**
     * 根据id获取代理
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取代理")
    @PreAuthorize("hasAuthority('area:query')")
    @ResponseBody
    public ClyjShopsArea get(@PathVariable String id) {
        return this.clyjShopsAreaService.selectById(id);
    }

    /**
     * 修改代理
     *
     * @param clyjShopsArea
     * @return
     */
    @PutMapping(value = "updateType")
    @ApiOperation(value = "修改地区代理")
    @PreAuthorize("hasAuthority('area:update')")
    public Integer updateType(@RequestBody ClyjShopsArea clyjShopsArea) {
        //根据传递过来的名称查询对应id
        QueryWrapper<ClyjUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", clyjShopsArea.getUserId());
        ClyjUser clyjUser = this.clyjUserMapper.selectOne(queryWrapper);
        clyjShopsArea.setUserId(clyjUser.getUserId());
        return this.clyjShopsAreaService.updateType(clyjShopsArea);
    }

}

